#parse the output of the HMM tool
package HmmOutputParser;
use IOutputParser;
use strict;
our @ISA = qw(IOutputParser);    # inherits from IOutputParser

#constructor
sub new {
    my ($class) = @_;
    #call the constructor of the parent class, IOutputParser.
    my $self = $class->SUPER::new();
    bless $self, $class;
    return $self;
}
# parse file (output file and allele file have the same format, so both use this function for parsing)
# Input: same as parseOutput or parseAlleles, and one more parameter:
# 		 fileType- "LABEL" or "ALLELE" (this parameter is needed in order to decide which IOutputParser function
#					should be called- addLabelCell or addAlleleCell)
sub _parseFile{
	my ($self, $fileName, $fileType) = @_;
	# read file
	open FILE, $fileName;
	my @outputData = <FILE>;
	close FILE;
	
	#first line in file contains names of the family memebres in the format "personx" (x is the person's numbers)
	my @peopleNames = split ' ' , $outputData[0];
	#next line is an emptyline- ignore
	shift @outputData; shift @outputData;
	#for every line in the file
	while ($#outputData > -1) {
		my $line = shift @outputData;
		shift @outputData;#next line is empty
		#split line value
		my @lineData1 = split (' ', $line);
		my @lineData = ();
		#ignore all unrelevant values (ignore "|")
		for my  $curValue1 (@lineData1) {
			if (not $curValue1=~ m/\|/){
				push(@lineData, $curValue1); 
			}
		}
		#first word in line is the marker name
		my $markerName = $lineData[0];
		(defined $markerName) || next;
		shift @lineData;
		my $cellIterator = 0;
		#iterate over all the values in this line and create relevant cells
		for my $person (@peopleNames) {		
			my $personNum = $person;
			#remove the word "person" from the name (and so stay only with person's number)
			$personNum = substr($personNum, 6);
			#create a suitable cell
			my $cellData = CellData->new();
			$cellData->val1($lineData[$cellIterator]);
			$cellData->val2($lineData[$cellIterator +1]);
			if ($fileType eq "LABEL"){
				$self->addLabelCell($cellData, $personNum, $markerName);
			}
			else {if ($fileType eq "ALLELE"){
				$self->addAlleleCell($cellData, $personNum, $markerName);
			}}
			$cellIterator= $cellIterator+2;	
			
		}
	}

}

#inherited from IOutputParser
sub parseOutput{
	my ($self, $datWrapper, $pedWrapper, $outputFile, $currentMarkers, $currFamily) = @_;
	$self->_parseFile($outputFile, "LABEL");
}

#inherited from IOutputParser
sub parseAlleles{
	my ($self, $allelsFile) = @_;
	$self->_parseFile($allelsFile, "ALLELE");
}
1;
